//
//  MVVMUserViewController.m
//  CharlesMVX
//
//  Created by Charles on 2020/4/1.
//  Copyright © 2020 Charles. All rights reserved.
//

#import "MVVMUserViewController.h"
#import "MVVMUserInfoController.h"
#import "BlogView.h"
#import "UIView+Alert.h"
#import "UIView+Extension.h"
#import "UIResponder+Router.h"
#import "MVCInfoViewController.h"
#import "DetailViewController.h"

@interface MVVMUserViewController ()

@property (nonatomic, assign) NSUInteger userId;
@property (nonatomic, strong) BlogView *blogVC;
@property (nonatomic, strong) MVVMUserInfoController *userInfoController;
@property (nonatomic, strong) MVCInfoViewController *userInfoVC;

@end

@implementation MVVMUserViewController

+ (instancetype)instanceWithUserId:(NSUInteger)userId{
    return [[MVVMUserViewController alloc]initWithUserId:userId];
}

- (instancetype)initWithUserId:(NSUInteger)userId {
    if (self = [super init]) {
        self.userId = userId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configuration];
    [self addUI];
    [self fetchData];
}

- (void)routeEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
    if ([eventName isEqualToString:LikeBlogEvent]) {
        BlogCellViewModel *viewModel = userInfo[kCellViewModel];
        if (!viewModel.isLiked) {
            [[viewModel.likeBlogCommand execute:nil] subscribeError:^(NSError *error) {
                [self showToastWithText:error.domain];
            }];
        }else {
            [self showAlertWithTitle:@"提示" message:@"确定取消点赞吗？" confirmHandler:^(UIAlertAction *confirmAction) {
                [[viewModel.likeBlogCommand execute:nil] subscribeError:^(NSError *error) {
                    [self showToastWithText:error.domain];
                }];
            }];
        }
    }
}

- (void)configuration {
    self.title = @"MVVM";
    self.view.backgroundColor = [UIColor whiteColor];
    
    @weakify(self);
    self.userInfoController = [MVVMUserInfoController instanceWithView:MVVMUserInfoView.new viewModel:[UserInfoViewModel viewModelWithUserId:self.userId]];
    
    [self.userInfoController setOnClickIconCommand:[[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.navigationController pushViewController:[[DetailViewController alloc]init] animated:YES];
        return [RACSignal empty];
    }]];
    
    self.blogVC = [BlogView instanceWithViewModel:[BlogViewModel viewModelWithUserId:self.userId]];
    [self.blogVC setDidSelectedRowCommand:[[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.navigationController pushViewController:[[DetailViewController alloc]init] animated:YES];
        return [RACSignal empty];
    }]];
}

- (void)addUI {
    CGFloat userInfoViewHeight = 160;
    self.userInfoController.view.frame = CGRectMake(0, NAVBAR_HEIGHT, SCREEN_WIDTH, userInfoViewHeight);
    [self.view addSubview:self.userInfoController.view];
    
    self.blogVC.tableView.frame = CGRectMake(0, self.userInfoController.view.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.userInfoController.view.bottom - NAVBAR_HEIGHT);
    
    [self.view addSubview:self.blogVC.tableView];
}

- (void)fetchData {
    [self.userInfoController fetchData];
    
    [self showHUD];
    [[self.blogVC.fetchDataCommand execute:nil] subscribeError:^(NSError *error) {
        [self hideHUD];
    }completed:^{
        [self hideHUD];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
